package com.ricchieri.manage.bean;

import java.util.Date;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.BasicFilter;
import com.ricchieri.filter.ReservationFilter;
import com.ricchieri.model.CancelationMotive;
import com.ricchieri.model.Operation;
import com.ricchieri.model.Reservation;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.ICancelationMotiveService;
import com.ricchieri.service.interf.ICancelationTypeService;
import com.ricchieri.service.interf.IOperationService;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.DateUtil;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "cancelReservationMB")
@ViewScoped
public class CancelReservationManageBean extends GenericManageBean {

	private static final long serialVersionUID = 5531296565698443473L;
	
	@ManagedProperty(value = "#{CancelationMotiveService}")
	private ICancelationMotiveService cancelationMotiveService;
	
	@ManagedProperty(value = "#{CancelationTypeService}")
	private ICancelationTypeService cancelationTypeService;
	
	@ManagedProperty(value = "#{UserService}")
	private IUserService userService;
	
	@ManagedProperty(value = "#{OperationService}")
	IOperationService operationService;	
	
	private Map<String, String> cancelationTypes;
	private String selectedCancelationType;
	
	private CancelationMotive cancelMotive = new CancelationMotive();
	private Reservation reservation;
	
	private Boolean block = Boolean.FALSE;
	private String blockMotive = "";
	private ReservationFilter reservationFilter;
	
	@PostConstruct
	public void init() {
		reservation = (Reservation) FacesUtil.getParametersFlash(BusinessSettings.KEY_RESERVATION);
		reservationFilter = (ReservationFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(reservation == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_RESERVATION);
		} else {			
			if(isInPenalty()) {
				FacesUtil.showErrorMessage("cancelationReservationForm", "adviceBlockUser");
				block = Boolean.TRUE;
				String travel = this.formatDateTime(DateUtil.converToDate(reservation.getScheduleCharter().getSchedule().getDate(), reservation.getScheduleCharter().getSchedule().getTime()));
				travel = travel.concat(" ").concat(FacesUtil.getMessage("of")).concat(" ").concat(reservation.getScheduleCharter().getTrip().getFormatTrip());
				
				String penalty = reservation.getUser().getBlockMotive();
				if(StringUtils.isEmpty(penalty)) {
					blockMotive = FacesUtil.getMessage("defaultBlockMessage").concat(travel);
				} else {					
					blockMotive = penalty.concat(",").concat(travel);
				}
				
			} else {
				FacesUtil.showInfoMessage("cancelationReservationForm", "adviceNotBlockUser");
			} 
		}
	}
	
	private Boolean isInPenalty() {
		Date actualDate = new Date(System.currentTimeMillis());
		Date travel = DateUtil.converToDate(reservation.getScheduleCharter().getSchedule().getDate(), reservation.getScheduleCharter().getSchedule().getTime());
		
		long difference = travel.getTime() - actualDate.getTime();
		long penalty = reservation.getScheduleCharter().getTrip().getTime().getTime();
		
		if(difference < penalty) {
			return Boolean.TRUE;
		} else {
			return Boolean.FALSE;
		} 
		
	}

	public Reservation getReservation() {
		return reservation;
	}

	public void setReservation(Reservation reservation) {
		this.reservation = reservation;
	}

	public CancelationMotive getCancelMotive() {
		return cancelMotive;
	}

	public void setCancelMotive(CancelationMotive cancelMotive) {
		this.cancelMotive = cancelMotive;
	}
	
	public String saveCancelation() {
		
		if(getBlock() && StringUtils.isEmpty(getBlockMotive())) {
			FacesUtil.addErrorMsg("cancelationReservationForm:blockMotive", "requiredBlockMotive");
			return BusinessSettings.UNSUCCESS;
		} else {
			
			BasicFilter cancelationTypeFilter = new BasicFilter();
			cancelationTypeFilter.setId(Integer.valueOf(getSelectedCancelationType()));
			
			getCancelMotive().setCancelationType(getCancelationTypeService().getCancelationById(cancelationTypeFilter));
			getCancelationMotiveService().addCancelation(getCancelMotive(), getReservation());		
			
			FacesUtil.addParametersFlash(BusinessSettings.KEY_RESERVATION, getReservation());
			
			if(getBlock()) {
				User user = getReservation().getUser();
				user.setBlock(Boolean.TRUE);
				user.setBlockMotive(getBlockMotive());
				getUserService().updateUser(user);
			}
			
			if(isInPenalty()) {
				Operation cancelationOperation = new Operation();
				cancelationOperation.setAutomatic(Boolean.TRUE);
				cancelationOperation.setOperationDate(new Date(System.currentTimeMillis()));
				cancelationOperation.setOperationType(BusinessSettings.OPERATION_CANCELATION);
				cancelationOperation.setAmount(reservation.getCost());
				
				
				String description = FacesUtil.getMessage("travelDate").concat(" : ").concat(DateUtil.formatDate(reservation.getScheduleCharter().getSchedule().getDate()));
				description = description.concat("\n").concat(FacesUtil.getMessage("travelTime")).concat(" : ").concat(DateUtil.formatTime(reservation.getScheduleCharter().getSchedule().getTime()));
				description = description.concat("\n").concat(FacesUtil.getMessage("tripOrientation")).concat(" : ").concat(reservation.getScheduleCharter().getTrip().getFormatTrip());
				
				cancelationOperation.setObservation(description);
				cancelationOperation.setUser(reservation.getUser());
				getOperationService().addOperation(cancelationOperation);
			}
			
			FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, reservationFilter);
			return BusinessSettings.SUCCESS;
		}
	}
	
	public IOperationService getOperationService() {
		return operationService;
	}

	public void setOperationService(IOperationService operationService) {
		this.operationService = operationService;
	}
	
	public String cancel() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, reservationFilter);
		return BusinessSettings.SEARCH;
	}

	public ICancelationMotiveService getCancelationMotiveService() {
		return cancelationMotiveService;
	}

	public void setCancelationMotiveService(
			ICancelationMotiveService cancelationMotiveService) {
		this.cancelationMotiveService = cancelationMotiveService;
	}

	public ICancelationTypeService getCancelationTypeService() {
		return cancelationTypeService;
	}

	public void setCancelationTypeService(
			ICancelationTypeService cancelationTypeService) {
		this.cancelationTypeService = cancelationTypeService;
	}

	public Map<String, String> getCancelationTypes() {
		if(cancelationTypes == null) {
			cancelationTypes = getCancelationTypeService().getComboCancelationTypes();
		}
		return cancelationTypes;
	}

	public void setCancelationTypes(Map<String, String> cancelationTypes) {
		this.cancelationTypes = cancelationTypes;
	}

	public String getSelectedCancelationType() {
		return selectedCancelationType;
	}

	public void setSelectedCancelationType(String selectedCancelationType) {
		this.selectedCancelationType = selectedCancelationType;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public Boolean getBlock() {
		return block;
	}

	public void setBlock(Boolean block) {
		this.block = block;
	}

	public String getBlockMotive() {
		return blockMotive;
	}

	public void setBlockMotive(String blockMotive) {
		this.blockMotive = blockMotive;
	}
}
